package com.leo.model;

/**
 * @ClassName ArrayQuene
 * @Description 基于数组实现队列
 * @Author wangss
 * @date 2022.01.30 09:59
 * @Version 1.0
 */
public class ArrayQuene {

    private String[] quene;

    private int n;

    private int count;

    private int pushCur;
    private int pollCur;

    public ArrayQuene(int n) {
        quene = new String[n];
        this.n = n;
        this.count = 0;
        this.pushCur = 0;
        this.pollCur = 0;
    }

    public boolean push(String itm) {
        // 队列满了
        if (count >= n) {
            return false;
        }

        if (pushCur >= n) {
            pushCur = 0;
        }

        quene[pushCur] = itm;
        pushCur++;
        count++;
        return true;
    }

    public String poll() {
        // 队列中没数据
        if (count <= 0) {
            return null;
        }

        if (pollCur >= n) {
            pollCur = 0;
        }

        String str = quene[pollCur];
        pollCur++;
        count--;
        return str;
    }

    public int getCount() {
        return count;
    }
}
